package com.neuedu.my12306.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neuedu.my12306.service.IStationInfoService;
import com.neuedu.my12306.service.impl.IStationInfoServiceImpl;
import com.neuedu.my12306.util.StringArrayUtil;

/**
 * Servlet implementation class StationDeleteServlet
 */
@WebServlet("/StationDeleteServlet.do")
public class StationDeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IStationInfoService iStationInfoService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StationDeleteServlet() {
        super();
        iStationInfoService=new IStationInfoServiceImpl();
       
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.在接收数据前进行编码转换(统一使用过滤器转换)
		String[] ids = request.getParameterValues("id");
		int[] id=StringArrayUtil.pareStringArray(ids);
		for (int i : id) {
			System.out.println(i);
		}
		boolean ret = iStationInfoService.deleteByIds(id);
		if (ret) {
			response.sendRedirect(request.getContextPath()+"/StationAdminServlet.do");
		}else {
			//删除失败，跳转到一个操作错误提示页
			//页面跳转使用重定向后，需要使用HttpSession传递数据
			request.getSession().setAttribute("MSG", "删除车站信息失败！");
			request.getSession().setAttribute("BACKURL", request.getContextPath()+"/StationAdminServlet.do");
			response.sendRedirect(request.getContextPath()+"/error.jsp");
		}
	}

}
